package com.ettractions.kiosk.mediators
{
	import com.ettractions.kiosk.components.BrochureBookingInfoView;
	import com.ettractions.kiosk.events.BrochureSelectedEvent;
	import com.ettractions.kiosk.events.PreRenderBrochureEvent;
	import com.ettractions.kiosk.vo.BrochureVO;
	
	import flashx.textLayout.conversion.TextConverter;
	
	import org.robotlegs.mvcs.Mediator;
			
	public class BrochureBookingInfoMediator extends Mediator
	{
		[Inject]
		public var view:BrochureBookingInfoView;
		
		override public function onRegister():void
		{
			eventDispatcher.addEventListener(PreRenderBrochureEvent.PRE_RENDER_BROCHURE, handlePreRenderBrochure);			
			eventDispatcher.addEventListener(BrochureSelectedEvent.BROCHURE_SELECTED, handleBrochureEvent);
		}

		private function strReplace(str:String, search:String, replace:String):String {
			return str.split(search).join(replace);
		}
		
		private function updateView(brochure:BrochureVO):void
		{
			var copy:String = brochure.media.bookingPhone;
			
			var phone:String = strReplace(copy, " ", "-");
			view.phonenumber.textFlow = TextConverter.importToFlow(phone, TextConverter.TEXT_FIELD_HTML_FORMAT);
		}
		
		private function handlePreRenderBrochure(event:PreRenderBrochureEvent):void
		{
			updateView(event.brochure);
		}
		
		private function handleBrochureEvent(event:BrochureSelectedEvent):void
		{
			if (event.brochure != null)
				updateView(event.brochure);
		}		

	}
}